package com.mutouren.modules.datapush;

import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.common.thread.ThreadPoolManager;
import com.mutouren.modules.datapush.netty.HttpServer;

public class App {
	
	private static HttpServer httpServer;
	private final static int defaultPort = 9080;
	private final static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	private final static Logger runLogger = LogManager.getLogger(LogAlias.SystemRun.name());	

	public static void main(String[] args) {
		try {
			runLogger.info("mtr-datapush-app run");			
			SpringContextHelper.loadSpringConfig();
			//Thread.sleep(10000);
			runHttpServer(args);
		} catch (Throwable t) {
			t.printStackTrace();
			errorLogger.error("mtr-datapush-app error", t);
		} finally {
			close();
		}		
	}
	
	private static void runHttpServer(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = defaultPort;
        }
        
        httpServer = new HttpServer(port);
        httpServer.run();
	}
	
	private static void close() {
		httpServer = null;
		
		try {
			SpringContextHelper.close();
			ThreadPoolManager.close();
		} catch(Throwable t) {
			t.printStackTrace();
		}			
		runLogger.info("mtr-datapush-app over");
		LogManager.close();
	}

}
